﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.IO;

namespace CPlase
{
    public static class InstanceStorage
    {
        public const byte FOLDER_BIT_COUNT = 6;

        private static string GetPath(LocalResource Storage, string Path)
        {
            var EQ = EQ1G.GetQuery("", Path, FOLDER_BIT_COUNT, FOLDER_BIT_COUNT);
            if (Path.StartsWith("/"))
            {
                Path = Path.Substring(1);
            }
            Path = Path.Replace("/", "\\");
            return System.IO.Path.Combine(Storage.RootPath, EQ.ShardNumber.ToString(), EQ.PartitionKey, Path);
        }

        public static string GetPath(string LocalResourceName, string Path)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            return GetPath(Storage, Path);
        }

        public static bool Save(string LocalResourceName, string Path, string Content)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            try
            {
                string DirectoryPath = System.IO.Path.GetDirectoryName(FinalPath);
                if (!Directory.Exists(DirectoryPath))
                {
                    Directory.CreateDirectory(DirectoryPath);
                }
                File.WriteAllText(FinalPath, Content);
                return true;
            }
            catch (Exception Ex) { return false; }
        }

        public static bool SerializeSave(string LocalResourceName, string Path, object Content)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            try
            {
                string DirectoryPath = System.IO.Path.GetDirectoryName(FinalPath);
                if (!Directory.Exists(DirectoryPath))
                {
                    Directory.CreateDirectory(DirectoryPath);
                }
                File.WriteAllText(FinalPath, Content.Serialize());
                return true;
            }
            catch (Exception Ex) { return false; }
        }

        public static bool Save(string LocalResourceName, string Path, byte[] Content)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            try
            {
                string DirectoryPath = System.IO.Path.GetDirectoryName(FinalPath);
                if (!Directory.Exists(DirectoryPath))
                {
                    Directory.CreateDirectory(DirectoryPath);
                }
                File.WriteAllBytes(FinalPath, Content);
                return true;
            }
            catch (Exception Ex) { return false; }
        }

        public static bool Exists(string LocalResourceName, string Path)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            return File.Exists(FinalPath);
        }

        public static DateTime GetLastModifiedUtc(string LocalResourceName, string Path)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            return File.GetLastWriteTimeUtc(FinalPath);
        }


        public static string Load(string LocalResourceName, string Path)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            return File.ReadAllText(FinalPath);
        }

        public static byte[] LoadBinary(string LocalResourceName, string Path)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            return File.ReadAllBytes(FinalPath);
        }

        public static T DownloadSerialized<T>(string LocalResourceName, string Path)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            string Content = File.ReadAllText(FinalPath);
            if (Content == null) return default(T);
            try
            {
                return Content.Deserialize<T>();
            }
            catch { return default(T); }
        }

        public static void DownloadToStream(string LocalResourceName, string Path, Stream target)
        {
            var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
            var FinalPath = GetPath(Storage, Path);
            var Bytes = File.ReadAllBytes(FinalPath);
            var SourceStream = File.Open(FinalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            SourceStream.CopyTo(target);
            target.Seek(0, SeekOrigin.Begin);
            SourceStream.Close();
        }

        public static bool Kill(string LocalResourceName, string Path)
        {
            try
            {
                var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
                var FinalPath = GetPath(Storage, Path);
                File.Delete(FinalPath);
                return true;
            }
            catch (Exception Ex) { return false; }
        }

        public static bool KillDirectory(string LocalResourceName, string Path)
        {
            try
            {
                var Storage = RoleEnvironment.GetLocalResource(LocalResourceName);
                var FinalPath = GetPath(Storage, Path);
                Directory.Delete(FinalPath, true);
                return true;
            }
            catch (Exception Ex) { return false; }
        }
    }
}
